﻿using System;
using System.ServiceModel;

namespace BizElements.BusinessLayer
{
    /// <summary>Non-reusable WCF ILogReceiverService client. Channel is disposed after every invocation.</summary>
    internal class WcfLogSender : ClientBase<ILogReceiverService>, ILogSender
    {
        public string Url
        {
            get
            {
                if (this.Endpoint != null && this.Endpoint.Address != null && this.Endpoint.Address.Uri != null)
                    return this.Endpoint.Address.Uri.OriginalString;
                else
                    return null;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                    return;

                bool hasChanged = (value != this.Url);
                if (hasChanged)
                    this.Endpoint.Address = new EndpointAddress(value);
            }
        }

        public void WriteLogEntry(byte[] serializedLogEntry)
        {
            try
            {
                base.Channel.WriteLogEntry(serializedLogEntry);
                Close();
            }
            catch
            {
                Abort();
                throw;
            }
        }
    }    
}
